<?php

// $Id: dbobject.inc 24 2010-11-11 07:33:55Z mjohnson60@gmail.com $
//
// $Author: mjohnson60@gmail.com $
// $Date: 2010-10-29 07:27:52 -0400 (Fri, 29 Oct 2010) $
//
// $Rev: 16 $
// 


/**
 * DBObject
 * 
 * This class is the parent class of the Developer, Milestone, Feature, and Task classes
 * 
 * It handles most of the work from the other classes. This class handles creating, updating, 
 * loading, etc. 
 * 
 * @package BugZapp
 * @author Mark JOhnson
 * @copyright 2010
 * @version 1.0
 * @access public
 */
class DBObject
{
	// extended classes will use $table to store the name of the table that holds their data
	var $table = NULL;
	
	// extended classes will use the $fields array to list the database fields in their respective tables
	// the array will be structured as $field_name => '[escape_string]' so that the DBObject class can execute
	// queries efficiently.
	var $fields = array();
	
	// extended classes will use $id_name to store the name of their ID field.
	// ie. the Developer class has $id_name = 'did';
	protected $id_name = 'id';

	/**
	 * DBObject::DBObject()
	 * 
	 * @param mixed $data
	 * @return void
	 */
	public function DBObject($data = NULL)
	{
		if (!empty($data))
		{
			if (is_numeric($data))
			{
				$data = db_fetch_array(db_query('SELECT * FROM ' . $this->table . ' WHERE ' . $this->id_name . '=%d', $data));
			}
			
			if (is_array($data))
			{
				foreach ($this->fields as $key => $value)
				{
					if (array_key_exists($key, $data))
					{
						$this->{$key} = $data[$key];
					}
				}
				
				if (isset($data[$this->id_name]))
				{
					// an object is being loaded from the database
					$this->id($data[$this->id_name]);
				}
			}
		}
	}

	/**
	 * DBObject::save()
	 * 
	 * This function is not implemented by any extended class.
	 * 
	 * This uses the variables in the $fields array to update and insert new objects
	 * into the database table--the name of the table is stored in $table
	 * 
	 * @return
	 */
	public function save()
	{
		if (empty($this->{$this->id_name}))
		{
			$sql = 'INSERT INTO ' . $this->table . ' (' . implode(', ', array_keys($this->fields)) . ') VALUES (' . implode(', ', array_values($this->fields)) . ')';
			$values = array();
			
			foreach ($this->fields as $key => $value)
			{
				$values[] = $this->{$key};
			}
			
			$result = db_query($sql, $values);
			
			$this->{$this->id_name} = db_result(db_query('SELECT MAX(' . $this->id_name . ') FROM ' . $this->table));
			
			return $result;
		}
		else
		{
			$values = array();
			$sql = 'UPDATE ' . $this->table . ' SET ';
			
			$i = 0;
			foreach ($this->fields as $key => $value)
			{
				if ($i > 0)
				{
					$sql .= ', ';
				}
				
				$sql .= $key . '=' . $value . ' ';
				$values[] = $this->{$key};
				
				$i++;
			}
			
			$sql .= 'WHERE ' . $this->id_name . '=%d';
			$values[] = $this->{$this->id_name};
			
			return db_query($sql, $values);
		}
	}

	/**
	 * DBObject::id()
	 * 
	 * Sets and returns the ID number for any DBObject and all DBObject types
	 * 
	 * @param mixed $id
	 * @return
	 */
	public function id($id = NULL)
	{
		if (isset($id))
		{
			$this->{$this->id_name} = $id;
		}
		
		return $this->{$this->id_name};
	}

	/**
	 * DBObject::schema()
	 * 
	 * Extended classes will store their table's schema representation here for installation
	 * 
	 * @return
	 */
	public static function schema()
	{
		return '';
	}

	/**
	 * DBObject::install()
	 * 
	 * Extended classes will use this function to install the table schema  
	 * 
	 * @return void
	 */
	public static function install()
	{
	
	}

	/**
	 * DBObject::uninstall()
	 * 
	 * @return void
	 */
	public static function uninstall()
	{
	
	}
}


